﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form5 : Form
    {
        static CookieCollection tmpCookies;

        public Form5()
        {
            InitializeComponent();
            tmpCookies = new CookieCollection();
        }

        private void GetPostData(StringBuilder sb, ref HttpWebRequest request)
        {
            string para = sb.ToString();

            StringBuilder UrlEncoded = new StringBuilder();
            Char[] reserved = { '?', '=', '&' };
            byte[] SomeBytes = null;
            if (para != null)
            {
                int i = 0, j;
                while (i < para.Length)
                {
                    j = para.IndexOfAny(reserved, i);
                    if (j == -1)
                    {
                        UrlEncoded.Append(HttpUtility.UrlEncode(para.Substring(i, para.Length - i), System.Text.Encoding.Default));
                        break;
                    }
                    UrlEncoded.Append(HttpUtility.UrlEncode(para.Substring(i, j - i), System.Text.Encoding.Default));
                    UrlEncoded.Append(para.Substring(j, 1));
                    i = j + 1;
                }
                SomeBytes = Encoding.Default.GetBytes(UrlEncoded.ToString());
                request.ContentLength = SomeBytes.Length;
                Stream newStream = request.GetRequestStream();
                newStream.Write(SomeBytes, 0, SomeBytes.Length);
                newStream.Close();
            }
        }

        /// <summary>
        /// denglu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string strUrl = "http://www.ifindu.cn/login.php?type=dologin";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "post";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1;  Embedded Web Browser from: http://bsalsa.com/; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 1.1.4322; .NET CLR 2.0.50727; CIBA; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
            request.CookieContainer = new CookieContainer();

            StringBuilder sb = new StringBuilder();
            sb.Append(@"nickname=13683246141");
            sb.Append("&password=198303");
            //sb.Append("&ExtraCode=" + yanzheng + "");
            //sb.Append("&x=26");
            //sb.Append("&y=13");

            GetPostData(sb, ref request);

            HttpWebResponse myHttpWebResponse = (HttpWebResponse)request.GetResponse();

            foreach (System.Net.Cookie newcookie in myHttpWebResponse.Cookies)
            {
                tmpCookies.Add(newcookie);
            }

            Stream receiveStream = myHttpWebResponse.GetResponseStream();
            Encoding encode = System.Text.Encoding.UTF8;
            StreamReader readStream = new StreamReader(receiveStream, System.Text.Encoding.GetEncoding("utf-8"));
            string strtemp = readStream.ReadToEnd();

            this.richTextBox1.Text = strtemp;

            this.webBrowser1.DocumentText = strtemp;
        }

        /// <summary>
        /// fanhui
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            string host2 = "http://www.ifindu.cn/home/";

            Uri URI = new Uri(host2);
            HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(host2);
            request2.Method = "get";

            //request2.ServicePoint.Expect100Continue = false;
            //request2.Timeout = 30000;
            //request2.KeepAlive = true;
            request2.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";
            //request2.Headers.Add("UA-CPU", "x86");
            //request2.Headers.Add("Accept-Encoding", "gzip, deflate");
            request2.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1;  Embedded Web Browser from: http://bsalsa.com/; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 1.1.4322; .NET CLR 2.0.50727; CIBA; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";

            request2.CookieContainer = new CookieContainer();

            foreach (System.Net.Cookie cookie in tmpCookies)
            {
                cookie.Domain = URI.Host;
                if (cookie.Value.Length != 0)
                {
                    request2.CookieContainer.Add(cookie);
                }
                else
                {
                    //Console.WriteLine(cookie.ToString());
                }
            }

            HttpWebResponse myHttpWebResponse = (HttpWebResponse)request2.GetResponse();
            Stream receiveStream = myHttpWebResponse.GetResponseStream();
            Encoding encode = System.Text.Encoding.UTF8;
            //StreamReader readStream = new StreamReader(receiveStream, encode);
            StreamReader readStream = new StreamReader(receiveStream, System.Text.Encoding.GetEncoding("utf-8"));

            string temp = readStream.ReadToEnd();
            this.richTextBox1.Text = temp;
            this.webBrowser1.DocumentText = temp;
            //Uri u = new Uri("http://www.ifindu.cn/home/");
            //this.webBrowser1.Url = u;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            string strUrl = "http://www.ifindu.cn/login.php";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1;  Embedded Web Browser from: http://bsalsa.com/; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 1.1.4322; .NET CLR 2.0.50727; CIBA; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
            request.CookieContainer = new CookieContainer();

            HttpWebResponse myHttpWebResponse = (HttpWebResponse)request.GetResponse();

            foreach (System.Net.Cookie newcookie in myHttpWebResponse.Cookies)
            {
                tmpCookies.Add(newcookie);
            }

            Stream receiveStream = myHttpWebResponse.GetResponseStream();
            Encoding encode = System.Text.Encoding.UTF8;
            StreamReader readStream = new StreamReader(receiveStream, encode);
            string strtemp = readStream.ReadToEnd();

            this.richTextBox1.Text = strtemp;

            this.webBrowser1.DocumentText = strtemp;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            tmpCookies = new CookieCollection();
        }
    }
}